package com.kou.controller;/**
 * @author SteinY
 * @create 2022-03-24 21:32
 * @description:
 */

import com.alibaba.fastjson.JSONObject;
import com.kou.pojo.Address;
import com.kou.pojo.Cart;
import com.kou.pojo.Orders;
import com.kou.pojo.User;
import com.kou.service.*;
import com.kou.service.impl.AddressServiceImpl;
import com.kou.service.impl.CartServiceImpl;
import com.kou.service.impl.OrderServiceImpl;
import com.kou.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:myshop2
 * author:SteinY
 * time:2022/3/24 21:32
 * description:
 */
@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    CartService cartService = new CartServiceImpl();
    AddressService addressService = new AddressServiceImpl();
    public String preView(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        List<Cart> carts = cartService.findCarts(uid);
        List<Address> addressList = addressService.findAll(uid);
        request.setAttribute("cartList",carts);
        request.setAttribute("addressList",addressList);
//        cartService.clear(uid);
        return Constants.FORWARD + "order.jsp";
    }

    public String create(HttpServletRequest request, HttpServletResponse response){
        String uid = request.getParameter("uid");
        String aid = request.getParameter("aid");
        String sum = request.getParameter("sum");
        orderService.create(uid,aid,sum);
        List<Orders> orders= orderService.findAll(uid);
//        cartService.clear(uid);
        request.setAttribute("ordersList",orders);
        return Constants.FORWARD + "orderList.jsp";
    }
    public String show(HttpServletRequest request, HttpServletResponse response){
        User user =(User) request.getSession().getAttribute("loginUser");
        int id = user.getId();
        String uid = String.valueOf(id);
        List<Orders> orders= orderService.findAll(uid);
//        cartService.clear(uid);
        request.setAttribute("ordersList",orders);
        return Constants.FORWARD + "orderList.jsp";
    }

    public void get(HttpServletRequest request, HttpServletResponse response){

        List<Orders> orders= orderService.showAll();
//        cartService.clear(uid);
        UserService userService = new UserServiceImpl();
        for (Orders order : orders) {
            order.setUsername(userService.findUserName(order.getUid()));
        }
        String orderJson = JSONObject.toJSONString(orders);
        try {
            response.getWriter().write(orderJson);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String detail(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");

        Orders order = orderService.findById(oid);

        request.setAttribute("order",order);
        return Constants.FORWARD + "orderDetail.jsp";
    }

    public String search(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String status = request.getParameter("orderStatus");
        System.out.println(status);
        UserService userService = new UserServiceImpl();
        User user = userService.findUserByUsername(username);
        List<Orders> orders = orderService.find(user.getId(),status);
        request.setAttribute("orderList",orders);
        return Constants.FORWARD + "admin/showAllOrder.jsp";
    }

}